import React, {
  useMemo, useEffect, createContext, useContext,
} from 'react';
import { inject, observer } from 'mobx-react';
import { injectIntl } from 'react-intl';
import { DataSet } from 'choerodon-ui/pro';
import useStore from './useStore';
import TableDs from './tableDataSet';

interface ContextProps{
    prefixCls:string,
    intlPrefix:string,
    dyTableStore:object,
    tableDs:DataSet,
    projectId:string,
    formatMessage:Function,
}
const Store = createContext({} as ContextProps);
export function useTableStore() {
  return useContext(Store);
}

export const StoreProvider = injectIntl(inject('AppState')((props:any) => {
  const {
    AppState,
    AppState: {
      currentMenuType: { projectId },
    },
    intl: { formatMessage },
    children,
  } = props;
  const intlPrefix = 'c7ncd.continuous.deployment.table';
  const dyTableStore = useStore();
  const tableDs = useMemo(() => new DataSet(TableDs({
    formatMessage,
    intlPrefix,
    projectId,
  })), [projectId]);
  const value = {
    ...props,
    intlPrefix,
    prefixCls: 'c7ncd-continuous-deployment-table',
    dyTableStore,
    projectId,
    formatMessage,
    tableDs,
  };
  return (
    <Store.Provider value={value}>
      {children}
    </Store.Provider>
  );
}));
